package cn.com.pc.content.service;

import cn.com.pc.content.domain.content.Content;
import cn.com.pc.content.domain.content.ContentType;
import cn.com.pc.content.domain.tag.TagHistory;
import cn.com.pc.content.domain.tag.TagHistoryRepository;
import cn.com.pc.content.dto.*;
import cn.com.pc.content.dto.Sort;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.*;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.time.LocalDateTime;

@Service
public class TagHistoryService {

    @Autowired
    private TagHistoryRepository tagHistoryRepository;


    /**
     * 物料打标记录
     * @param content
     * @param contentType
     * @return
     */
    public Long createTagHistory(Content content, ContentDTO contentDTO, ContentType contentType) {
        String tagBy = contentDTO.getTaggedBy() != null ? contentDTO.getTaggedBy() : content.getUpdatedBy();
        LocalDateTime taggedAt = contentDTO.getTaggedAt() != null ? contentDTO.getTaggedAt() : content.getUpdatedAt();
        TagHistory tagHistory = new TagHistory();
        tagHistory.setContentId(content.getId());
        tagHistory.setContentType(contentType);
        tagHistory.setSite(content.getSite());
        tagHistory.setTagBy(tagBy);
        tagHistory.setTagAt(taggedAt);
        tagHistory.setTagIds(content.getTagIds());
        TagHistory history = tagHistoryRepository.save(tagHistory);
        return history.getId();
    }


    /**
     * 查询打标记录
     * @param tagHistoryDTO
     * @param pageForm
     * @param sort
     * @return
     */
    public PageResult<TagHistory> listTagHistoryByDTO(TagHistoryDTO tagHistoryDTO, PageForm pageForm, Sort sort) {
        //查询对象
        TagHistory tagHistory = new TagHistory();
        if (tagHistoryDTO.getId() != null) {
            tagHistory.setId(tagHistoryDTO.getId());
        }
        if (StringUtils.hasLength(tagHistoryDTO.getContentId())) {
            tagHistory.setContentId(tagHistoryDTO.getContentId());
        }
        if (tagHistoryDTO.getContentType() != null) {
            tagHistory.setContentType(tagHistoryDTO.getContentType());
        }
        if (tagHistoryDTO.getSite() != null) {
            tagHistory.setSite(tagHistoryDTO.getSite());
        }
        if (StringUtils.hasLength(tagHistoryDTO.getTagBy())) {
            tagHistory.setTagBy(tagHistoryDTO.getTagBy());
        }
        if (tagHistoryDTO.getTagAt() != null) {
            tagHistory.setTagAt(tagHistoryDTO.getTagAt());
        }

        Example<TagHistory> example = Example.of(tagHistory);
        Pageable pageable;
        if (!StringUtils.hasLength(sort.getSortBy())) {
            sort.setSortBy("id");
        }
        pageable = PageRequest.of(pageForm.getPageNum() - 1, pageForm.getPageSize(), sort.getDirection(), sort.getSortBy());
        Page<TagHistory> tagHistoryPage = tagHistoryRepository.findAll(example, pageable);

        return PageResult.fromPage(tagHistoryPage);
    }

}
